package com.space.school.dao.impl;

import com.space.school.dao.RegisterDao;
import com.space.school.domain.User;
import com.space.school.utils.SpaceJDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Date;
import java.util.List;

public class RegisterDaoImpl implements RegisterDao {
    //        连接数据库
    JdbcTemplate jdbcTemplate = new JdbcTemplate(SpaceJDBCUtils.getDs());
    @Override
    public boolean register(String username, String password) {
//        sql语句
        String sql = "INSERT INTO user(userName,passWord,createTime) VALUES (?,?,?)";
        jdbcTemplate.update(sql,username,password,new Date());
        return true;
    }

    @Override
    public boolean usernameExists(String username) {
        String sql = "SELECT * FROM user WHERE username= ?";
        List<User> list=jdbcTemplate.query(sql,new BeanPropertyRowMapper<User>(User.class),username);
        if(list.isEmpty()){
            return true;
        }
        return false;
    }

    @Override
    public boolean passwordCheck(String password) {
        String regex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d).{6,}$";
        boolean flag = password.matches(regex);
        if (flag){
            return true;
        }
        return false;
    }
}
